package com.rem.rabbitmq.boot.CbackupExchange;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Rem
 * @date 2022/1/8
 */

@Configuration
public class BackupExchangeConfig {

    /**
     * 普通交换机（发布确认） 队列 routingKey
     */
    public static final String NORMAL_EXCHANGE = "NORMAL_EXCHANGE";

    public static final String NORMAL_ROUTING_KEY = "NORMAL_ROUTING_KEY";

    public static final String NORMAL_QUEUE = "NORMAL_QUEUE";

    /**
     * 备份交换机 备份队列 报警队列
     */
    public static final String BACKUP_EXCHANGE = "BACKUP_EXCHANGE";

    public static final String BACKUP_QUEUE = "BACKUP_QUEUE";

    public static final String WARING_QUEUE = "WARING_QUEUE";


    /**
     * 声明备份交换机
     *
     * @return
     */
    @Bean("backupExchange")
    public FanoutExchange backupExchange() {
        return ExchangeBuilder.fanoutExchange(BACKUP_EXCHANGE).build();
    }

    /**
     * 声明备份队列
     *
     * @return
     */
    @Bean("backupQueue")
    public Queue backupQueue() {
        return QueueBuilder.durable(BACKUP_QUEUE).build();
    }

    /**
     * 声明报警队列
     *
     * @return
     */
    @Bean("waringQueue")
    public Queue waringQueue() {
        return QueueBuilder.durable(WARING_QUEUE).build();
    }

    /**
     * 声明备份队列绑定关系
     *
     * @param backupQueue
     * @param backupExchange
     * @return
     */
    @Bean
    public Binding backupQueueBinding(@Qualifier("backupQueue") Queue backupQueue, @Qualifier("backupExchange") FanoutExchange backupExchange) {
        return BindingBuilder.bind(backupQueue).to(backupExchange);
    }

    /**
     * 声明报警队列绑定关系
     *
     * @param waringQueue
     * @param backupExchange
     * @return
     */
    @Bean
    public Binding waringQueueaBinding(@Qualifier("waringQueue") Queue waringQueue, @Qualifier("backupExchange") FanoutExchange backupExchange) {
        return BindingBuilder.bind(waringQueue).to(backupExchange);
    }


    /**
     * 声明业务交换机
     * **绑定备份交换机
     *
     * @return
     */
    @Bean("normalExchange")
    public DirectExchange normalExchange() {
        return ExchangeBuilder.directExchange(NORMAL_EXCHANGE).alternate(BACKUP_EXCHANGE).build();
    }

    /**
     * 声明发布确认队列
     *
     * @return
     */
    @Bean("normalQueue")
    public Queue normalQueue() {
        return QueueBuilder.durable(NORMAL_QUEUE).build();
    }

    /**
     * 声明普通绑定关系
     *
     * @param normalQueue
     * @param normalExchange
     * @return
     */
    @Bean
    public Binding normalQueueaBinding(@Qualifier("normalQueue") Queue normalQueue, @Qualifier("normalExchange") DirectExchange normalExchange) {
        return BindingBuilder.bind(normalQueue).to(normalExchange).with(NORMAL_ROUTING_KEY);
    }


}
